package cassys.domain.classManagement;

import java.io.Serializable;
import java.util.Collection;
import cassys.domain.assessment.*;
import java.util.HashSet;

/**
 * @author Thurein
 */
public class Module implements Serializable {

    private Long moduleId;
    private ModuleType moduleType;
    private String moduleCode;
    private String title;
    private Lecture owner;
    private Collection<Lecture> lectures;
    private Collection<Assessment> assessments;

    public Module() {
        this.lectures = new HashSet<Lecture>();
        this.assessments = new HashSet<Assessment>();
        System.out.println("accessableLectures initialized with new array list");
    }

    public Module(String moduleCode, String title, ModuleType moduleType, Lecture owner) {
        this();

        this.moduleCode = moduleCode;
        this.title = title;
        this.moduleType = moduleType;
        this.setOwner(owner);
        System.out.println("new modulized initialized");
    }

    public void swapOwner(Lecture owner) {
        removeCurrentOwner();
        setOwner(owner);
    }

    public void addLecture(Lecture lecture) {
        if (!this.lectures.contains(lecture)) {
            this.getLectures().add(lecture);
            lecture.addModule(this);
        }
    }

    public boolean hasAssessments() {
        return this.assessments.size() > 0;
    }

    public Collection<Assessment> getAssessments() {
        return this.assessments;
    }

    public ModuleType getType() {
        return moduleType;
    }

    public String getModuleCode() {
        return moduleCode;
    }

    public void setModuleCode(String moduleCode) {
        this.moduleCode = moduleCode;
    }

    public String getTitle() {
        return title;
    }

    public Collection<Lecture> getLectures() {
        return this.lectures;
    }

    public Long getModuleId() {
        return moduleId;
    }

    public void setModuleId(Long moduleId) {
        this.moduleId = moduleId;
    }

    private void removeCurrentOwner() {
        this.getLectures().remove(this.getOwner());
        this.getOwner().removeOwnership(this);
    }

    public Lecture getOwner() {
        return owner;
    }

    public void setOwner(Lecture owner) {
        this.owner = owner;

        if (!this.lectures.contains(owner)) {
            this.getLectures().add(owner);
        }

        this.owner.setModuleOwner(this);
    }

    @Override
    public boolean equals(Object obj) {
        System.out.println("check equality for Modules");
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Lecture)) {
            return false;
        }
        final Module other = (Module) obj;
        System.out.println("Others : " + other.getModuleCode());
        System.out.println("this :" + this.moduleCode);

        System.out.println(this.moduleCode.equalsIgnoreCase(other.getModuleCode()));
        return this.moduleCode.equalsIgnoreCase(other.getModuleCode());
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 79 * hash + (this.getModuleCode() != null ? this.getModuleCode().hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "Module{" + "moduleId=" + moduleId + ", moduleType=" + moduleType + ", moduleCode=" + moduleCode + ", title=" + title + ", owner=" + owner + ", lectures=" + getLectures() + ", assessments=" + assessments + '}';
    }

    public void addAssessment(Assessment assessment) {
        this.assessments.add(assessment);
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }
}
